package com.kate.mall.order.price;

import cn.hutool.core.map.MapUtil;
import com.kate.mall.order.dto.OrderDTO;
import com.kate.mall.order.dto.OrderItemDTO;
import com.kate.mall.order.feign.MallFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 含赠品的运费计算组件
 * @author kate2chan
 *
 */
@Component
@Slf4j
public class GiftIncludedFreightCalculator implements FreightCalculator {
	
	/**
	 * 物流中心接口
	 */
	@Autowired
	private MallFeignClient mallFeignClient;
	
	/**
	 * 计算运费
	 */
	@Override
	public BigDecimal calculate(OrderDTO order, OrderItemDTO orderItem,
								PromotionActivityResult result) {
		BigDecimal resultFreight =new BigDecimal("0.0");
		
		//freight +=
		BigDecimal freight = MapUtil.get(mallFeignClient.getFreight(),"freight", BigDecimal.class);
		BigDecimal freightLimit = MapUtil.get(mallFeignClient.getFreightLimit(),"freight_limit", BigDecimal.class);
		BigDecimal payGoodsPrice = order.getGoodsPrice();
		log.info("=====含赠品的运费计算组件，计算参数:运费：{},免运费价：{},商品总价：{}",freight,freightLimit,payGoodsPrice);
		if(payGoodsPrice.compareTo(freightLimit)==-1){
			return new BigDecimal("8");
		}
		for(OrderItemDTO giftItem : result.getOrderItems()) {
			if(giftItem.getPrice().compareTo(freightLimit)==-1){
				resultFreight = resultFreight.add(freight);
			}
		}
		
		return freight;
	}

}